import cv2

camera = cv2.VideoCapture(0)
if not camera.isOpened():
    print("无法打开摄像头")
    exit()
else:
    print("摄像头已打开")
    #输出摄像头信息
    print(camera.getBackendName())
    print(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
    print(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
    print(camera.get(cv2.CAP_PROP_FPS))
    print(camera.get(cv2.CAP_PROP_BRIGHTNESS))
# 利用摄像头拍照
ret, frame = camera.read()
if ret:
    #在当前目录保存图片
    cv2.imwrite("photo.jpg", frame)
    #输出图片信息
    print(frame.shape)
    print("拍照成功")
else:
    print("拍照失败")
# 释放摄像头
camera.release()
# 加载图像
image = cv2.imread("azy.jpg", cv2.IMREAD_GRAYSCALE)

# 调整图像大小
scale_percent = 0.2  # 调整比例
width = int(image.shape[1] * scale_percent)
height = int(image.shape[0] * scale_percent)
dim = (width, height)
resized_image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)

# 定义ASCII字符集
ascii_chars = "@%#*+=-:. "

# 转换图像为ASCII字符画
ascii_image = ""
for row in resized_image:
    for pixel in row:
        ascii_index = pixel // 32  # 根据像素值映射到字符集中
        ascii_image += ascii_chars[ascii_index]
    ascii_image += "\n"

# 显示ASCII字符画
print(ascii_image)
